package cn.edu.lit.jsjxy.CourseExample.chapter2;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.edu.lit.jsjxy.CourseExample.R;

public class ToastTestActivity extends Activity implements View.OnClickListener
{

    Button btnOrdinary, btnCustomPos, btnCustomView1, btnCustomView2;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);

        btnOrdinary = findViewById(R.id.btnOrdinary);
        btnCustomPos = findViewById(R.id.btnCustomPos);
        btnCustomView1 = findViewById(R.id.btnCustomView1);
        btnCustomView2 = findViewById(R.id.btnCustomView2);

        btnOrdinary.setOnClickListener(this);
        btnCustomPos.setOnClickListener(this);
        btnCustomView1.setOnClickListener(this);
        btnCustomView2.setOnClickListener(this);
    }


    @Override
    public void onClick(View view)
    {
        switch (view.getId()) {
            case R.id.btnOrdinary:
                makeOrdinaryToast("test");
                break;
            case R.id.btnCustomPos:
                makeCustomPosToast(200, 200);
                break;
            case R.id.btnCustomView1:
                makeCustomToast1();
                break;
            case R.id.btnCustomView2:
                makeCustomToast2();
                break;
        }
    }

    public void makeOrdinaryToast(String msg)
    {
        Toast t = Toast.makeText(this, "", Toast.LENGTH_LONG);
        t.setText(msg);
        t.setDuration(Toast.LENGTH_LONG);
        t.show();
    }

    public void makeCustomPosToast(int x, int y)
    {
        Toast t = Toast.makeText(this, "OK", Toast.LENGTH_LONG);
        t.setGravity(Gravity.CENTER, x, y);
        t.show();
    }

    public void makeCustomToast1()
    {
        Toast t = new Toast(this);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        ImageView iv = new ImageView(this);
        iv.setImageResource(R.mipmap.ic_launcher_round);

        layout.addView(iv);

        TextView tv = new TextView(this);
        tv.setText("hello");
        tv.setTextSize(24);
        tv.setTextColor(Color.RED);

        layout.addView(tv);

        t.setView(layout);

        t.show();
    }

    public void makeCustomToast2()
    {
        Toast t = new Toast(this);

        LayoutInflater inflater = getLayoutInflater();

        View view = inflater.inflate(R.layout.lite, null);
        view.setBackgroundColor(Color.BLUE);

        t.setView(view);

        t.setGravity(Gravity.CENTER, 0, 0);


        t.show();
    }
}
